package com.example.mxs.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.mxs.dao.UserRepository;
import com.example.mxs.domain.User;
import com.example.mxs.service.TokenService;
import com.example.mxs.service.UserService;
import com.example.mxs.token.UserLoginToken;
import com.example.mxs.util.MetaData;
import com.example.mxs.util.ResponseData;


import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
public class UserController {

    @Autowired
    TokenService tokenService;
    @Autowired
    UserService userService;


    @UserLoginToken
    @ApiOperation(value = "获取用户信息", notes = "根据id获取用户信息")
    @GetMapping("/api/v1/users/{id}")
    public ResponseData getUser(@PathVariable("id") String id) {
        ResponseData responseData= new ResponseData();
        User user=userService.findUserById(Integer.parseInt(id));
        if(user!=null){
             responseData.setData(user);
             responseData.setMeta(MetaData.CODE_200);
        }
        else {
            responseData.setData(MetaData.CODE_404);
        }
        return responseData;
    }

    @UserLoginToken
    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PutMapping("/api/v1/users/{id}")
    public ResponseData updateUser(@PathVariable("id") String id,@RequestBody Map<String, String> params) {
        String email= params.get("email");
        String mobile= params.get("mobile");
        int _id=Integer.parseInt(id);
        User user= userService.findUserById(_id);
        if(email!=null){
            user.setEmail(email);
        }
        if(mobile!=null){
            user.setMobile(mobile);
        }
        User _user= userService.update(_id,user);
        return new ResponseData(_user,MetaData.CODE_200);
    }

   // @UserLoginToken
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping("/api/v1/users")
    public ResponseData getUsers(@RequestParam(required = false) String query, @RequestParam int pagenum, @RequestParam int pagesize) {
        JSONObject jsonObject = new JSONObject();
        Page<User> pageObj = userService.findAllUser(pagenum, pagesize);
        List<User> users = pageObj.getContent();
        jsonObject.put("users", users);
        jsonObject.put("totalpage", pageObj.getTotalPages());
        jsonObject.put("pagenum", pageObj.getNumber());
        ResponseData responseData = new ResponseData();
        responseData.setData(jsonObject);
        responseData.setMeta(MetaData.CODE_200);
        return responseData;
    }

    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping( "/api/v1/users")
    public ResponseData addUser(@RequestBody Map<String, String> params) {

        User user = new User();
        user.setUsername(params.get("username"));
        user.setPassword(params.get("password"));
        user.setEmail(params.get("email"));
        user.setMobile(params.get("mobile"));
        User _user =userService.addUser(user);

        ResponseData responseData = new ResponseData();
        responseData.setData(_user);
        responseData.setMeta(MetaData.CODE_201);

        return responseData;
    }

    @UserLoginToken
    @ApiOperation(value="删除用户", notes="根据id删除用户")
    @DeleteMapping( "/api/v1/users/{id}")
    public ResponseData deleteUser(@PathVariable("id") String id)
    {
        userService.delete(Integer.parseInt(id));
        return new ResponseData(MetaData.CODE_200);
    }

    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping( "/api/v1/login")
    public Object login(@RequestBody Map<String, String>  param) {
        String username=param.get("username");
        String password=param.get("password");
        ResponseData responseData = new ResponseData();
        User userForBase = userService.findUserByName(username);
        if (userForBase == null) {
            //jsonObject.put("message","登录失败,用户不存在");
            responseData.setMeta(MetaData.CODE_401);
            return responseData;
        } else {
            if (!userForBase.getPassword().equals(password)) {
                //  jsonObject.put("message","登录失败,密码错误");
                responseData.setMeta(MetaData.CODE_401);
                return responseData;
            } else {
                String token = tokenService.getToken(userForBase);

                JSONObject data = new JSONObject();
                data.put("user", userForBase);
                data.put("token", token);
                responseData.setData(data);
                responseData.setMeta(MetaData.CODE_200);
                return responseData;
            }
        }
    }
}
